package com.goldcn.goods.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.goods.dao.GoodsAttrStoneMapper;
import com.goldcn.goods.dto.GoodsSkuExtDto;
import com.goldcn.goods.dto.PageInfo;
import com.goldcn.goods.dto.QueryGoodsAttrStoneDto;
import com.goldcn.goods.model.GoodsSku;
import com.goldcn.interceptor.SystemContext;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Created by NIK on 2018/7/3.
 */
@Service
public class GoodsHighQueryService {

    @Autowired
    GoodsAttrStoneMapper goodsAttrStoneMapper;

    @Autowired
    GoodsService goodsService;

    public Object selectList(QueryGoodsAttrStoneDto queryDto) {

        String orderBy = "addTime";
        String orderType = "desc";
        queryDto.setCompanyID(SystemContext.getCompanyID()+"");
        if (CommonUtils.isNotEmpty(queryDto.getSort())) {
            orderBy = queryDto.getSort();
        }
        if (CommonUtils.isNotEmpty(queryDto.getSortOrder()) && queryDto.getSortOrder().equals("asc")) {
            orderType = queryDto.getSortOrder();
        }
        String searchSource =  "info.";
        if (orderBy.equals("weight")){
            searchSource="stone.";
        }
        String orderStr = searchSource + orderBy + " " + orderType;
        PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize(), orderStr);//分页初始化
        Page page = (Page) goodsAttrStoneMapper.selectCondition(queryDto);

        return new PageInfo(page, page.getPages(), page.getTotal());


    }

    public Object selectShopList(QueryGoodsAttrStoneDto queryDto) {


        String orderBy = "addTime";
        String orderType = "desc";
        queryDto.setCompanyID(SystemContext.getCompanyID()+"");
        if (CommonUtils.isNotEmpty(queryDto.getSort())) {
            orderBy = queryDto.getSort();
        }
        if (CommonUtils.isNotEmpty(queryDto.getSortOrder()) && queryDto.getSortOrder().equals("asc")) {
            orderType = queryDto.getSortOrder();
        }
        String searchSource =  "info.";
        if (orderBy.equals("weight")){
            searchSource="stone.";
        }
        String orderStr =searchSource + orderBy + " " + orderType;
        if (CommonUtils.isNotEmpty(queryDto.getSortOrder()) && queryDto.getSortOrder().equals("seqno")) {//根据标签排序的特殊标记
            orderStr = "label.seqno asc";
        }

        PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize(), orderStr);//分页初始化
        Page page = (Page) goodsAttrStoneMapper.selectShopCondition(queryDto);


        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    public Object initHighQuery() {

        int highGoods = 0;
        //判断是否需要高级查询 start
        String highQuery = goodsService.queryHighQueryClazzID()+"";
        if(CommonUtils.isNotEmpty(highQuery)){
            String[] highQuerys = highQuery.split(",");
            List<String> highQueryGoods= Arrays.asList(highQuerys);
            List<GoodsSkuExtDto> skus = goodsAttrStoneMapper.selectHighQueryGoods(highQueryGoods);
            if(CollectionUtils.isNotEmpty(skus) && skus.size()>0){
                highGoods = goodsService.saveGoodsAttrStone(skus);
            }
        }
        return highGoods;
    }
}
